package com.xjrsoft.module.iot.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.iot.dto.IotDicPageDto;
import com.xjrsoft.module.iot.entity.IotDic;
import com.xjrsoft.module.iot.entity.IotGroup;
import com.xjrsoft.module.iot.entity.IotLink;
import com.xjrsoft.module.iot.service.IIotDicService;
import com.xjrsoft.module.iot.vo.IotDicPageVo;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tzx
 * @since 2024-04-10
 */
@RestController
@RequestMapping(GlobalConstant.IOT_MODULE_PREFIX + "/dic")
@AllArgsConstructor
public class IotDicController {

    private final IIotDicService iotDicService;

    @GetMapping("/list")
    @ApiOperation(value = "获取数据（不分页）")
    public R list() {

        List<IotDic> list = iotDicService.list(Wrappers.lambdaQuery(IotDic.class)
                .select(IotDic.class, x -> VoToColumnUtil.fieldsToColumns(IotDic.class).contains(x.getProperty())));
        return R.ok(list);
    }



    @GetMapping(value = "/page")
    @ApiOperation(value="查询字典 (分页)")
    public R page(@Valid IotDicPageDto dto){

        MPJLambdaWrapper<IotDic> select = MPJWrappers.<IotDic>lambdaJoin()
                .leftJoin(IotLink.class, IotLink::getId, IotDic::getLinkId)
                .leftJoin(IotGroup.class, IotGroup::getId, IotDic::getGroupId)
                .eq(ObjectUtil.isNotNull(dto.getGroupId()), IotDic::getGroupId, dto.getGroupId())
                .like(StrUtil.isNotBlank(dto.getKeyword()), IotDic::getName, dto.getKeyword())
                .selectAs(IotLink::getName, IotDicPageVo::getLinkName)
                .selectAs(IotGroup::getName, IotDicPageVo::getGroupName)
                .orderByDesc(IotDic::getId)
                .select(IotDic::getId)
                .select(IotDic.class, x -> VoToColumnUtil.fieldsToColumns(IotDic.class).contains(x.getProperty()));

        IPage<IotDicPageVo> page = iotDicService.selectJoinListPage(ConventPage.getPage(dto), IotDicPageVo.class, select);
        PageOutput<IotDicPageVo> pageOutput = ConventPage.getPageOutput(page, IotDicPageVo.class);
        return R.ok(pageOutput);


//
//        LambdaQueryWrapper<IotDic> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper
//                .eq(ObjectUtil.isNotNull(dto.getGroupId()),IotDic::getGroupId,dto.getGroupId())
//                .like(StrUtil.isNotBlank(dto.getKeyword()),IotDic::getName,dto.getKeyword())
//                .orderByDesc(IotDic::getId)
//                .select(IotDic.class,x -> VoToColumnUtil.fieldsToColumns(IotDic.class).contains(x.getProperty()));
//
//
//        IPage<IotDic> page = iotDicService.page(ConventPage.getPage(dto), queryWrapper);
//        PageOutput<IotDic> pageOutput = ConventPage.getPageOutput(page, IotDic.class);
//        return R.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id 字典详情")
    public R info(@RequestParam Long id){
        IotDic dic = iotDicService.getById(id);
        if (dic == null) {
            return R.error("找不到此数据！");
        }
        return R.ok(dic);
    }


    @PostMapping
    @ApiOperation(value = "新增 字典")
    @Transactional(rollbackFor = Exception.class)
    public R add(@Valid @RequestBody IotDic dto){
        return R.ok(iotDicService.addIotDic(dto));

    }

    @PutMapping
    @ApiOperation(value = "修改互联网连接")
    public R update(@Valid @RequestBody IotDic dto){
        return R.ok(iotDicService.updateIotDic(dto));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@Valid @RequestBody List<Long> ids){
        return R.ok(iotDicService.removeBatchByIds(ids));
    }
}
